package com.supereye.platform.web.sys.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.supereye.platform.common.ResultMsg;
import com.supereye.platform.common.SysConstant;
import com.supereye.platform.web.sys.model.SysUser;
import com.supereye.platform.web.sys.service.LoginService;

@Controller
@RequestMapping("/login")
public class LoginController {
	@Resource 
	private LoginService loginService;
	
	@RequestMapping("/login")
	@ResponseBody
	public ResultMsg login(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		try{
			SysUser user = loginService.login(username,password);
			request.getSession().setAttribute(SysConstant.LOGIN_USER, user);
			return new ResultMsg(ResultMsg.SUCCESS,"登陆成功");
		}catch(Exception e){
			e.printStackTrace();
			return new ResultMsg(ResultMsg.FAIL,e.getMessage());
		}
	}
	
	@RequestMapping("/loginout")
	@ResponseBody
	public ResultMsg loginout(HttpServletRequest request,HttpServletResponse response) throws Exception{
		try{
			request.getSession().removeAttribute(SysConstant.LOGIN_USER);
			return new ResultMsg(ResultMsg.SUCCESS,"注销成功");
		}catch(Exception e){
			e.printStackTrace();
			return new ResultMsg(ResultMsg.FAIL,e.getMessage());
		}
	}
}
